home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1122 / 1122.xpi / chrome / tabmixplus.jar / content / tabmixplus / flst / lasttab.js < prev   
Text File  |  2009-09-06  |  14KB  |  372 lines

  1. /*////////////////////////////////////////////////////////////////////
  2. // The Original Code is the "LastTab" extension for Mozilla Firefox.//
  3. // version 1.5 - October 26, 2005                                  //
  4. // The Initial Developer of the Original Code is Timothy Humphrey. //
  5. *////////////////////////////////////////////////////////////////////
  6. var TMP_LastTab = {
  7.    CtrlKey : false,
  8.    favorLeftToRightOrdering : true,
  9.    handleCtrlTab : true,
  10.    KeyboardNavigating : true,
  11.    KeyLock : false,
  12.    respondToMouseInTabList : true,
  13.    showTabList : true,
  14.    SuppressTabListReset : false,
  15.    TabHistory : [],
  16.    TabIndex : 0,
  17.    TabList : null,
  18.    TabListLock : false,
  19.    _inited: false,
  20.  
  21.    DisplayTabList : function() {
  22.       var element = document.documentElement;
  23.       var tablist = this.TabList;
  24.       var menuitem, tab, imageUrl, x, y, i, activeIndex;
  25.  
  26.       createCommonList(tablist, this.handleCtrlTab ? 3 : 2);
  27.  
  28.       var item = tablist.childNodes[this.TabIndexToMenuItem()];
  29.       item.setAttribute("_moz-menuactive", "true");
  30.       updateMenuItemActive(null, item);
  31.  
  32.       //moveTo() method introduces anomalies, e.g. hovering over location bar moves the popup; hiding    and showing the popup works better
  33.       document.popupNode = element;
  34.       x = -element.boxObject.screenX;
  35.       y = 10000;
  36.       tablist.showPopup(element, x, y, "popup", null, null); //show offscreen to get popup measurements
  37.       x = Math.round((window.outerWidth - tablist.boxObject.width) / 2) - (element.boxObject.screenX - window.screenX);
  38.       y = Math.round((window.outerHeight - tablist.boxObject.height) / 2) - (element.boxObject.screenY - window.screenY);
  39.       if(x + element.boxObject.screenX < 0)
  40.          x = -element.boxObject.screenX;
  41.       else if(x + element.boxObject.screenX + tablist.boxObject.width > window.screen.availWidth)
  42.          x = window.screen.availWidth - tablist.boxObject.width - element.boxObject.screenX;
  43.       if(y + element.boxObject.screenY < 0)
  44.          y = -element.boxObject.screenY;
  45.       else if(y + element.boxObject.screenY + tablist.boxObject.height > window.screen.availHeight)
  46.          y = window.screen.availHeight - tablist.boxObject.height - element.boxObject.screenY;
  47.       if(x == -1 && y == -1) //workaround special status of -1, -1 position in showPopup() method
  48.          x = y = 0;
  49.       this.SuppressTabListReset = true;
  50.       tablist.hidePopup();
  51.       this.SuppressTabListReset = false;
  52.       tablist.showPopup(element, x, y, "popup", null, null);
  53.  
  54.       var ietab = "chrome://ietab/content/reloaded.html?url="
  55.       if (gBrowser.currentURI.spec.indexOf(ietab) == 0)
  56.          tablist.focus();
  57.  
  58.       this.TabListLock = true;
  59.    },
  60.  
  61.    init : function() {
  62.       this._inited = true;
  63.       var browser = document.documentElement;
  64.  
  65.       this.TabList = document.getElementById("lasttabTabList");
  66.  
  67.       gBrowser.mTabBox._eventNode.removeEventListener("keypress", gBrowser.mTabBox, false);
  68.       browser.addEventListener("keydown", this, true);
  69.       browser.addEventListener("keypress", this, true);
  70.       browser.addEventListener("keyup", this, true);
  71.       this.TabList.addEventListener("DOMMenuItemActive", this, true);
  72.       this.TabList.addEventListener("DOMMenuItemInactive", this, true);
  73.  
  74.       var tab = gBrowser.mCurrentTab;
  75.       tab.__LastTab = true;
  76.       this.TabHistory.push(tab);
  77.       if (tab._tPos != 0) {
  78.          // if session manager select other tab then the first one we need to build TabHistory in two steps
  79.          // to maintain natural Ctrl-Tab order.
  80.          // we call MaintainTabHistory from here the 2nd time is call from OnSelect.
  81.          this.MaintainTabHistory(tab._tPos);
  82.       }
  83.  
  84.       this.ReadPreferences();
  85.    },
  86.  
  87.    deinit : function() {
  88.       var browser = document.documentElement;
  89.       browser.removeEventListener("keydown", this, true);
  90.       browser.removeEventListener("keypress", this, true);
  91.       browser.removeEventListener("keyup", this, true);
  92.       this.TabList.removeEventListener("DOMMenuItemActive", this, true);
  93.       this.TabList.removeEventListener("DOMMenuItemInactive", this, true);
  94.    },
  95.  
  96.    handleEvent : function(event) {
  97.       switch (event.type) {
  98.          case "keydown":
  99.             this.OnKeyDown(event);
  100.             break;
  101.          case "keypress":
  102.             this.OnKeyPress(event);
  103.             break;
  104.          case "keyup":
  105.             this.OnKeyUp(event);
  106.             break;
  107.          case "DOMMenuItemActive":
  108.             this.ItemActive(event);
  109.             break;
  110.          case "DOMMenuItemInactive":
  111.             this.ItemInactive(event);
  112.             break;
  113.       }
  114.    },
  115.  
  116.    ItemActive : function(event) {
  117.       updateMenuItemActive(event);
  118.       if(this.respondToMouseInTabList) {
  119.          if(this.KeyboardNavigating) {
  120.             if(event.target.value != this.inverseIndex(this.TabIndex))
  121.                this.TabList.childNodes[this.TabIndexToMenuItem()].setAttribute("_moz-menuactive", "false");
  122.             this.KeyboardNavigating = false;
  123.          }
  124.          this.TabIndex = this.inverseIndex(event.target.value);
  125.       }
  126.       else {
  127.          if(event.target.value != this.inverseIndex(this.TabIndex))
  128.             event.target.setAttribute("_moz-menuactive", "false");
  129.       }
  130.    },
  131.  
  132.    ItemInactive : function(event) {
  133.       updateMenuItemInactive(event);
  134.       if(!this.respondToMouseInTabList && event.target.value == this.inverseIndex(this.TabIndex))
  135.          event.target.setAttribute("_moz-menuactive", "true");
  136.    },
  137.  
  138.    MaintainTabHistory : function(lastIndex) {
  139.       var newTabs = [], tab, i;
  140.  
  141.       // Gather tab synchronization info
  142.       if (typeof(lastIndex)=="undefined")
  143.         lastIndex = gBrowser.mTabContainer.childNodes.length;
  144.       for(i = 0; i < lastIndex; i++) {
  145.          tab = gBrowser.mTabContainer.childNodes[i];
  146.          if(!tab.__LastTab)
  147.             newTabs[newTabs.length] = tab;
  148.       }
  149.  
  150.       // Purge old tab info from history
  151.       i = 0;
  152. try{
  153.       while(i < this.TabHistory.length) {
  154.          if(!this.TabHistory[i] || this.TabHistory[i].parentNode != gBrowser.mTabContainer)
  155.             this.TabHistory.splice(i, 1);
  156.          else
  157.             i++;
  158.       }
  159. } catch (e) {tmLog("error from Ctrl+Tab in MaintainTabHistory " + typeof(this.TabHistory[i]) + "\n" + e
  160. + "\n" + "this.TabHistory.length " + this.TabHistory.length + "\n" + "i " + i);}
  161.  
  162.       // Add new tabs to history
  163.       if(newTabs.length > 0) {
  164.          tab = this.TabHistory.pop();
  165.          if(this.favorLeftToRightOrdering) {
  166.             for(i = newTabs.length - 1; i >= 0; i--) {
  167.                newTabs[i].__LastTab = true;
  168.                this.TabHistory.push(newTabs[i]);
  169.             }
  170.          }
  171.          else {
  172.             for(i = 0; i < newTabs.length; i++) {
  173.                newTabs[i].__LastTab = true;
  174.                this.TabHistory.push(newTabs[i]);
  175.             }
  176.          }
  177.          this.TabHistory.push(tab);
  178.       }
  179.    },
  180.  
  181.    OnKeyDown : function(event) {
  182.       this.CtrlKey = event.ctrlKey && !event.altKey && !event.metaKey;
  183.    },
  184.  
  185.    OnKeyPress : function _LastTab_OnKeyPress(event) {
  186.       var tabCount = this.handleCtrlTab ? this.TabHistory.length : gBrowser.mTabs.length;
  187.  
  188.       if((this.handleCtrlTab || this.showTabList) && event.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_TAB && event.ctrlKey && !event.altKey && !event.metaKey) {
  189.          if(!this.KeyLock) {
  190.             if (this.handleCtrlTab) {
  191.                this.MaintainTabHistory();
  192.                tabCount = this.TabHistory.length;
  193.                this.TabIndex = tabCount - 1;
  194.             } else {
  195.                this.TabIndex = tabCount - 1 - gBrowser.mCurrentTab._tPos;
  196.             }
  197.             this.KeyLock = true;
  198.          }
  199.  
  200.          if(this.TabListLock)
  201.             this.TabList.childNodes[this.TabIndexToMenuItem()].setAttribute("_moz-menuactive", "false");
  202.  
  203.          if(event.shiftKey) {
  204.             this.TabIndex++;
  205.             if(this.TabIndex >= tabCount)
  206.                this.TabIndex = 0;
  207.          }
  208.          else {
  209.             this.TabIndex--;
  210.             if(this.TabIndex < 0)
  211.                this.TabIndex = tabCount - 1;
  212.          }
  213.  
  214.          if(this.showTabList) {
  215.             this.KeyboardNavigating = true;
  216.             if(!this.TabListLock) {
  217.                if(tabCount > 1)
  218.                   this.DisplayTabList();
  219.             }
  220.             else {
  221.                var item = this.TabList.childNodes[this.TabIndexToMenuItem()];
  222.                item.setAttribute("_moz-menuactive", "true");
  223.                updateMenuItemActive(null, item);
  224.             }
  225.          }
  226.          else {
  227.             TMP_tabSelectedFromList(this.TabHistory[this.TabIndex]);
  228.          }
  229.  
  230.          event.stopPropagation();
  231.          event.preventDefault();
  232.       }
  233.       else {
  234.          if(this.TabListLock)
  235.             this.TabList.hidePopup();
  236.  
  237.          gBrowser.mTabBox.handleEvent(event);
  238.       }
  239.    },
  240.  
  241.    OnKeyUp : function _LastTab_OnKeyUp(event) {
  242.       var keyReleased = event.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_CONTROL;
  243.  
  244.       this.CtrlKey = event.ctrlKey && !event.altKey && !event.metaKey;
  245.       if(keyReleased && this.TabListLock) {
  246.          if(this.TabList.childNodes[this.TabIndexToMenuItem()].getAttribute("_moz-menuactive") == "true") {
  247.             var tabToSelect = this.handleCtrlTab ? this.TabHistory[this.TabIndex] : gBrowser.mTabs[gBrowser.mTabs.length - 1 - this.TabIndex];
  248.             TMP_tabSelectedFromList(tabToSelect);
  249.          }
  250.  
  251.          updateMenuItemInactive(null);
  252.          gBackupLabel=="";
  253.  
  254.          this.PushSelectedTab();
  255.          this.TabList.hidePopup();
  256.       }
  257.       if(keyReleased && this.KeyLock) {
  258.          this.PushSelectedTab();
  259.          this.TabIndex = 0;
  260.          this.KeyLock = false;
  261.       }
  262.    },
  263.  
  264.    OnMenuCommand : function _LastTab_OnMenuCommand(event) {
  265.       if(this.respondToMouseInTabList) {
  266.          var tabToSelect = this.handleCtrlTab ? this.TabHistory[event.target.value] : gBrowser.mTabs[event.target.value];
  267.          TMP_tabSelectedFromList(tabToSelect);
  268.          this.PushSelectedTab();
  269.       }
  270.    },
  271.  
  272.    OnPopupHidden : function() {
  273.       if(!this.SuppressTabListReset) {
  274.          var tablist = this.TabList;
  275.  
  276.          while(tablist.childNodes.length > 0)
  277.             tablist.removeChild(tablist.childNodes[0]);
  278.  
  279.          this.TabListLock = false;
  280.          this.TabIndex = 0;
  281.          this.KeyLock = false;
  282.  
  283.          hideCommonList(tablist);
  284.       }
  285.    },
  286.  
  287.    OnSelect : function() {
  288.       // session manager can select new tab before TMP_LastTab is init
  289.       if (!this._inited)
  290.          return;
  291.  
  292.       var tabCount = this.TabHistory.length;
  293.  
  294.       this.MaintainTabHistory();
  295.       if(tabCount != gBrowser.mTabContainer.childNodes.length) {
  296.          if(tabCount > gBrowser.mTabContainer.childNodes.length) {
  297.             if(gBrowser.mTabContainer.childNodes.length == 1) {
  298.                this.KeyLock = false;
  299.                this.TabIndex = 0;
  300.             }
  301.          }
  302.          this.PushSelectedTab();
  303.       }
  304.       else if(!this.KeyLock) {
  305.          if(this.CtrlKey)
  306.             this.KeyLock = true; //allow other tab navigation methods to work
  307.          else
  308.             this.PushSelectedTab();
  309.       }
  310.    },
  311.  
  312.    PushSelectedTab : function() {
  313.       var selectedTab = gBrowser.mTabContainer.selectedItem;
  314.       var tabCount, i;
  315.  
  316.       if(this.TabHistory[this.TabHistory.length - 1] != selectedTab) {
  317.          tabCount = this.TabHistory.length;
  318.          for(i = 0; i < tabCount; i++) {
  319.             if(this.TabHistory[i] == selectedTab) {
  320.                this.TabHistory.push(this.TabHistory.splice(i, 1)[0]);
  321.                break;
  322.             }
  323.          }
  324.       }
  325.    },
  326.  
  327.    ReadPreferences : function() {
  328.      /*
  329.       * Build-in tabPreviews exist in Firefox 3.5+
  330.       * when tabPreviews is on we disable our own function
  331.       *
  332.       * we use TMP_getBoolPref to make sure browser.ctrlTab.previews exist (for version before Firefox 3.5)
  333.       */
  334.       var mostRecentlyUsed = TMP_getBoolPref("", "browser.ctrlTab.previews", true);
  335.       var tabPreviews = document.getElementById("ctrlTab-panel") && "ctrlTab" in window;
  336.       if (tabPreviews) {
  337.          var tabPreviewsCurentStatus = ctrlTab._recentlyUsedTabs ? true : false;
  338.          tabPreviews = mostRecentlyUsed && tabxPrefs.getBoolPref("lasttab.tabPreviews");
  339.          if (tabPreviewsCurentStatus != tabPreviews) {
  340.             if (tabPreviews) {
  341.                ctrlTab.init();
  342.                ctrlTab._recentlyUsedTabs = [];
  343.                for (var i = 0; i < this.TabHistory.length; i++) {
  344.                   ctrlTab._recentlyUsedTabs.unshift(this.TabHistory[i]);
  345.                }
  346.             }
  347.             else
  348.                ctrlTab.uninit();
  349.          }
  350.       }
  351.  
  352.       this.handleCtrlTab = !tabPreviews && mostRecentlyUsed;
  353.       this.showTabList = !tabPreviews && tabxPrefs.getBoolPref("lasttab.showTabList");
  354.       this.favorLeftToRightOrdering = tabxPrefs.getBoolPref("lasttab.favorLeftToRightOrdering");
  355.       this.respondToMouseInTabList = tabxPrefs.getBoolPref("lasttab.respondToMouseInTabList");
  356.    },
  357.  
  358.    TabIndexToMenuItem : function() {
  359.       var activeIndex;
  360.       if (this.handleCtrlTab)
  361.          activeIndex = this.TabHistory.length - 1 - this.TabIndex;
  362.       else
  363.          activeIndex = gBrowser.mTabs.length - 1 - this.TabIndex;
  364.       return activeIndex;
  365.    },
  366.  
  367.    inverseIndex : function(index) {
  368.       return this.handleCtrlTab ? index : gBrowser.mTabs.length - 1 - index;
  369.    }
  370.  
  371. }
  372.